package com.gemalto.Kryptik.Gui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.*;


public class KeyedAlgoPanel extends ConcreteAlgoPanel implements ActionListener {

	protected KeyedAlgo algo;
	private CountedComboBox keyCombo;
	private int historyMax;
	private String helpText;
	private JRadioButton b4,b5;
	protected JPanel bottomPanel, keyPanel;


	/* Load settings from ini-file.
	 * Maximum HISTORY_MAX keys is loaded. If there is no HISTORY_MAX,
	 * we will set it to the default value 5.
	 */
	private void getHistory() {
	   String key;
	   int i, lastKey;


		// Download last keys to combo box
	   historyMax = Util.getIni().getIntValue(algo.toString(),"HISTORY_MAX");
	   if (historyMax>-1){
	   	for (i=0;i<historyMax;i++){
			key = Util.getIni().getValue(algo.toString(),"KEY_"+i);
			if (key!=null) keyCombo.insertItemAt(key,i);
		}
	   } else {
		// There is no HISTORY_MAX, lets set default value 5
	    	Util.getIni().setValue(algo.toString(),"HISTORY_MAX",5);
			historyMax=5;
	   }	


		// Set last used key active
		lastKey = Util.getIni().getIntValue(algo.toString(),"LAST_KEY");
		if ( lastKey >-1 ) {
			key = Util.getIni().getValue(algo.toString(),"KEY_"+lastKey);
			//keyCombo.setSelectedIndex(lastKey-1);
			if ( key!=null ) setKey(key);
		}

		
	}


	public KeyedAlgoPanel(KeyedAlgo algo, String text){
		this.algo=algo;
		this.helpText=text;
		initGui();
        getHistory();
	}


	public void initGui(){
	
		// Create GUI ...
		setLayout(new BoxLayout(this,1));
		keyPanel=new JPanel();
		keyPanel.setLayout(new BoxLayout(keyPanel,0));
		keyPanel.setBorder(new EmptyBorder(5,5,0,3));
	
	    // KeyLength is in bytes but HexDoument expect number of characters
		keyCombo = new CountedComboBox(algo.getKeyLength()*2, NewGui.getCounter());
		keyCombo.setEditable(true);
		keyCombo.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
		keyCombo.setMinimumSize(new Dimension(50, 25));  
		keyCombo.setPreferredSize(new Dimension(75, 25)); 
		keyCombo.setMaximumRowCount(5);

		((JTextField)keyCombo.getEditor()).addFocusListener( new FocusAdapter(){

			public void focusLost(FocusEvent e){
				setKey((String)keyCombo.getEditor().getItem());				
			}
		});

/*
        JButton keyButton = new JButton("Ok");
		keyButton.setMaximumSize(new Dimension(50, 25));
		keyButton.setMinimumSize(new Dimension(50, 25));  
		keyButton.setPreferredSize(new Dimension(50, 25)); 
        keyButton.setActionCommand("KEYB-OK");
		keyButton.addActionListener(this);
*/		
		keyPanel.add(new JLabel(Util.getTranslation("GENALGO_KEY")));
		keyPanel.add(keyCombo);
//		keyPanel.add(keyButton);

		bottomPanel=new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel,0));
		bottomPanel.setBorder(new EmptyBorder(2,2,0,0));
	
		if ( helpText!=null){
			ImageIcon icon = Util.getIcon("Inform.gif");
			JLabel textL = new JLabel(helpText,icon, JLabel.CENTER);
			textL.setVerticalTextPosition(JLabel.CENTER);
			textL.setIconTextGap(10);
			bottomPanel.add(textL);
		}

		add(keyPanel);
		add(bottomPanel);
		add(Box.createVerticalGlue());

	}


	protected int findKeyInCombo(String key){
		int i=-1;
		String tmpKey;
			
   		for (i=keyCombo.getItemCount()-1;i>=0;i--){
			tmpKey=(String)keyCombo.getItemAt(i);
			if (tmpKey!=null && tmpKey.equals(key)){
				break;
			}
   		}
		return i;
	}


	/* If the key is not in the ComboBox, we will insert it.
	 * History list in INI file is updated.
	 */
	protected void setKey(String key){
	     	// Add only new key ..
			int i;

			
			if ((i=findKeyInCombo(key))==-1){
				keyCombo.insertItemAt(key,0);
				i=0;
			}
			keyCombo.setSelectedIndex(i);

		
			Util.getIni().setValue(algo.toString(), "LAST_KEY",i );


     		// reset key list ...
     		for (i=0;i<keyCombo.getItemCount();i++){
			if (i>=historyMax) break;
			Util.getIni().setValue(algo.toString(), "KEY_"+i, (String)keyCombo.getItemAt(i));
     		}


			// ... and finally set key for this algo
			algo.setKey(key);
	}





	public void actionPerformed(ActionEvent e){
		if (e.getActionCommand().substring(0,4).equals("KEYB")){
			setKey((String)keyCombo.getEditor().getItem());
		}
	}


	/**
	 * This functions updates GUI of the KeyedAlgoPanel. Actually current key (<CODE>algo.getKey()</CODE>)
	 * is searched in combo box and if it is found, corresponding item of combo is selected, or new item
	 * is inserted otherwise.
	 */

	public void update(){
		int i;
		String key=algo.getKey();
	    if (key!=null){		
				if ((i=findKeyInCombo(key))==-1){
					keyCombo.insertItemAt(key,0);
					i=0;
				}
				keyCombo.setSelectedIndex(i);
		}
	}


}

// vim:tabstop=4:nowrap 
